package com.zzty.zzlp.controller;

import com.zzty.zzlp.entity.Article;
import com.zzty.zzlp.entity.User;
import com.zzty.zzlp.serviceimpl.EditServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;


@RestController

public class EditController {
    @Autowired
    EditServiceImpl es;

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public String getmsg(HttpServletRequest req) {
        User u = (User) req.getSession().getAttribute("User");
        int id;
        id = u.getId();
        String title = req.getParameter("title");
        String author = req.getParameter("author");
        String summary = req.getParameter("summary");
        String content = req.getParameter("msg");
        String img = req.getParameter("tu");
        String tt = req.getParameter("tt");

        if (img == null || img == "")
            img = "";
        Article ac = new Article();
        //得到long类型当前时间
        long l = System.currentTimeMillis();
//new日期对象
        Date date1 = new Date(l);
//转换提日期输出格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = dateFormat.format(date1);
        ac.setTitle(title);
        ac.setAuthor(author);
        ac.setSummary(summary);
        ac.setContent(content);
        ac.setUserId(id);
        ac.setTime(date);
        ac.setImgUrl(img);
        ac.setComment(tt);
        System.out.println(title);
        System.out.println(author);
        System.out.println(summary);
        System.out.println(content);
        System.out.println(date);
        int a = es.insert(ac);
        System.out.println(a);
        return "success";
    }
}
